import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'profile_logic.dart';
import '../routes.dart';
import '../../../../core/controllers/auth_controller.dart';

/// 个人信息页面
class SettingsProfileView extends GetView<SettingsProfileLogic> {
  const SettingsProfileView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor:  Colors.black,
      appBar: AppBar(
        backgroundColor: const Color(0xFF282528),
        elevation: 0,
        leading: GestureDetector(
          onTap: () => Get.back(),
          child: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
            size: 20,
          ),
        ),
        title: const Text(
          '个人信息',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
        ),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(height: 40.h),
            // 头像
            Center(
              child: GestureDetector(
                onTap: () => controller.pickAvatar(),
                child: Stack(
                  children: [
                    Obx(() => Container(
                          width: 100.w,
                          height: 100.w,
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            color: const Color(0xFF666666),
                            image: controller.avatarUrl.value.isNotEmpty
                                ? DecorationImage(
                                    image: controller.avatarUrl.value
                                            .startsWith('http')
                                        ? NetworkImage(
                                                controller.avatarUrl.value)
                                            as ImageProvider
                                        : FileImage(
                                            File(controller.avatarUrl.value)),
                                    fit: BoxFit.cover,
                                  )
                                : null,
                          ),
                          child: controller.avatarUrl.value.isEmpty
                              ? const Icon(Icons.person,
                                  size: 50, color: Colors.white)
                              : null,
                        )),
                    Positioned(
                      right: 0,
                      bottom: 0,
                      child: Container(
                        width: 32.w,
                        height: 32.w,
                        decoration: const BoxDecoration(
                          color: Color(0xFF007AFF),
                          shape: BoxShape.circle,
                        ),
                        child: const Icon(
                          Icons.camera_alt,
                          color: Colors.white,
                          size: 18,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(height: 40.h),
            // 设置项列表
            Container(
              margin: EdgeInsets.symmetric(horizontal: 24.w),
              decoration: BoxDecoration(
                color: const Color(0xFF282528),
                borderRadius: BorderRadius.circular(28.r),
              ),
              child: Column(
                children: [
                  _buildListItem(
                      '账号安全', '', () => Get.toNamed(SettingsRoutes.security)),
                  _buildDivider(),
                  Obx(() =>
                      _buildListItem('昵称', controller.nickname.value, () async {
                        final result = await Get.toNamed(
                            SettingsRoutes.nickname,
                            arguments: controller.nickname.value);
                        if (result != null) {
                          controller.nickname.value = result;
                        }
                      })),
                  _buildDivider(),
                  Obx(() =>
                      _buildListItem('职业', controller.career.value, () async {
                        final result = await Get.toNamed(SettingsRoutes.career,
                            arguments: controller.career.value);
                        if (result != null) {
                          controller.career.value = result;
                        }
                      })),
                  _buildDivider(),
                  Obx(() => _buildListItem('生日', controller.birthday.value,
                      () => controller.selectBirthday())),
                  _buildDivider(),
                  Obx(() => _buildListItem('性别', controller.gender.value,
                      () => controller.selectGender())),
                  _buildDivider(),
                  Obx(() => _buildListItem(
                      '身高',
                      controller.height.value > 0
                          ? '${controller.height.value} cm'
                          : '',
                      () => controller.selectHeight())),
                  _buildDivider(),
                  Obx(() => _buildListItem(
                      '体重',
                      controller.weight.value > 0
                          ? '${controller.weight.value} kg'
                          : '',
                      () => controller.selectWeight())),
                ],
              ),
            ),
            SizedBox(height: 40.h),
            // 退出账号按钮
            Container(
              margin: EdgeInsets.symmetric(horizontal: 24.w),
              width: double.infinity,
              height: 50.h,
              child: ElevatedButton(
                onPressed: _showLogoutDialog,
                style: ElevatedButton.styleFrom(
                  backgroundColor: const Color(0xFF282528),
                  foregroundColor: Colors.white,
                  elevation: 0,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(25.r),
                  ),
                ),
                child: Text(
                  '退出账号',
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
            SizedBox(height: 40.h),
          ],
        ),
      ),
    );
  }

  Widget _buildListItem(String title, String value, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        height: 50.h,
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Row(
          children: [
            Text(
              title,
              style: TextStyle(
                color: Colors.white,
                fontSize: 16.sp,
              ),
            ),
            const Spacer(),
            if (value.isNotEmpty)
              Text(
                value,
                style: TextStyle(
                  color: const Color(0xFF666666),
                  fontSize: 14.sp,
                ),
              ),
            SizedBox(width: 8.w),
            const Icon(
              Icons.arrow_forward_ios,
              color: Color(0xFF666666),
              size: 16,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDivider() {
    return Divider(
      height: 1,
      thickness: 1,
      color: const Color(0xFF1A1A1A),
      indent: 20.w,
      endIndent: 20.w,
    );
  }

  /// 显示退出登录确认对话框
  void _showLogoutDialog() {
    Get.dialog(
      AlertDialog(
        backgroundColor: const Color(0xFF2A2A2A),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(16.r),
        ),
        title: Text(
          '退出账号',
          style: TextStyle(
            color: Colors.white,
            fontSize: 18.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
        content: Text(
          '确定要退出当前账号吗？',
          style: TextStyle(
            color: Colors.grey[400],
            fontSize: 14.sp,
          ),
        ),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text(
              '取消',
              style: TextStyle(
                color: Colors.grey[400],
                fontSize: 14.sp,
              ),
            ),
          ),
          TextButton(
            onPressed: () {
              Get.back();
              // 调用退出登录方法
              AuthController.to.logout();
            },
            child: Text(
              '确定',
              style: TextStyle(
                color: const Color(0xFFFF6B6B),
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
